<?php
/**
 * Created by PhpStorm.
 * User: 仙女大人
 * Date: 2019/6/13
 * Time: 10:15
 */

namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller
{
    // 登录页面
    public function login()
    {
        return $this->fetch();
    }

    // 处理登录页面
    public function login_do()
    {
        $name	= input('get.user_name');
        if(empty($name)){
            $this->error("请输入用户名");
        }
        $password 	= input('get.password');
        if(empty($password)){
            $this->error("请输入密码");
        }

        $code = input('get.captcha');
        if(empty($code)){
            $this->error("请输入验证码");
        }

        $user = Db::name("user")->where("user_name",$name)->where("password",$password)->find();
        if($user){
            // Session::set('id', $user['id']);
            Session::set('user', $user);

            return json(['code' => 200, 'data' => $user, 'msg' => 'ok']);
        }else{
            return json(['code' => 0,  'msg' => '账号密码错误']);
        }
    }
    /*
     * 管理员退出
     */
    public function loginout(){
        session('user_name',null);//让session的值为空
        $this->redirect('/admin/login/login');
    }
}